package com.cookor.poi.modules.phy.service;

import com.cookor.poi.common.persistence.DataEntity;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.service.CrudService;
import com.cookor.poi.common.utils.JedisUtils;
import com.cookor.poi.common.utils.WorkDayUtils;
import com.cookor.poi.modules.phy.dao.TArticleDao;
import com.cookor.poi.modules.phy.dto.TSchedueMpDto;
import com.cookor.poi.modules.phy.entity.TArticle;
import com.cookor.poi.modules.phy.entity.TArticleType;
import com.cookor.poi.modules.phy.entity.TBookNo;
import com.cookor.poi.modules.phy.entity.TSchedueTemplate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class TArticleService extends CrudService<TArticleDao, TArticle> {
  @Autowired
  private TArticleTypeService tArticleTypeService;
  
  public TArticle get(String id) {
    return (TArticle)super.get(id);
  }
  
  @Transactional(readOnly = false)
  public void addHits(String id) {
    ((TArticleDao)this.dao).addHits(id);
  }
  
  public List<TArticle> findList(TArticle tArticle) {
    return super.findList( tArticle);
  }
  
  public Page<TArticle> findPage(Page<TArticle> page, TArticle tArticle) {
    return super.findPage(page,  tArticle);
  }
  
  @Transactional(readOnly = false)
  public void save(TArticle tArticle) {
    super.save( tArticle);
    TArticleType tArticleType = new TArticleType();
    tArticleType.setTenantId(Integer.valueOf(5));
    tArticleType.setDelFlag("0");
    List<TArticleType> typeList = this.tArticleTypeService.findList(tArticleType);
    Iterator<TArticleType> it = typeList.iterator();
    while (it.hasNext()) {
      TArticleType type = it.next();
      if (type.getTypeName().equals("系统公告")) {
        it.remove();
        continue;
      } 
      tArticle = new TArticle();
      tArticle.setTenantId(Integer.valueOf(5));
      tArticle.setType(Integer.valueOf(type.getId()));
      tArticle.setDelFlag("0");
      type.setArticleList(findPage(new Page(1, 3), tArticle).getList());
    } 
    JedisUtils.setObject("articleList", typeList, 1800);
  }
  
  @Transactional(readOnly = false)
  public void delete(TArticle tArticle) {
    super.delete( tArticle);
  }
  
  public static List<TSchedueMpDto> formart(List<TBookNo> bookNoList) throws Exception {
    List<TSchedueMpDto> dtos = new ArrayList<>();
    Calendar c = Calendar.getInstance();
    for (TBookNo bookNo : bookNoList) {
      TSchedueMpDto dto = new TSchedueMpDto();
      c.set(1, bookNo.getBookYear().intValue());
      c.set(2, bookNo.getBookMonth().intValue() - 1);
      c.set(5, bookNo.getBookDay().intValue());
      dto.setStock(Integer.valueOf(1));
      dto.setDay(bookNo.getBookDay());
      dto.setMonth(bookNo.getBookMonth());
      dto.setYear(bookNo.getBookYear());
      dto.setTimeType((bookNo.getType().intValue() == 1) ? "上午" : "下午");
      dto.setWeek(WorkDayUtils.getChineseWeek(c));
      dtos.add(dto);
    } 
    return dtos;
  }
  
  private Integer getWeekStr(Integer nowWeek, TSchedueTemplate tSchedueTemplate) {
    if (nowWeek.intValue() == 1)
      return tSchedueTemplate.getSun(); 
    if (nowWeek.intValue() == 2)
      return tSchedueTemplate.getMon(); 
    if (nowWeek.intValue() == 3)
      return tSchedueTemplate.getTue(); 
    if (nowWeek.intValue() == 4)
      return tSchedueTemplate.getWed(); 
    if (nowWeek.intValue() == 5)
      return tSchedueTemplate.getThu(); 
    if (nowWeek.intValue() == 6)
      return tSchedueTemplate.getFri(); 
    if (nowWeek.intValue() == 7)
      return tSchedueTemplate.getSat(); 
    return null;
  }
}
